package org.linlinjava.litemall.wx.web;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.validation.constraints.NotNull;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.linlinjava.litemall.core.util.ResponseUtil;
import org.linlinjava.litemall.core.validator.Order;
import org.linlinjava.litemall.core.validator.Sort;
import org.linlinjava.litemall.db.domain.LitemallUserApprove;
import org.linlinjava.litemall.wx.annotation.LoginUser;
import org.linlinjava.litemall.wx.service.WxQualificationsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.google.common.collect.Maps;

/**
 * 商户资质服务
 */
@RestController
@RequestMapping("/wx/qualifications")
@Validated
public class WxQualificationsController {
    private final Log logger = LogFactory.getLog(WxQualificationsController.class);

    @Autowired
    private WxQualificationsService wxQualificationsService;

    /**
     * 用户个人页面数据
     * <p>
     * 目前是用户订单统计信息
     *
     * @param userId 用户ID
     * @return 用户个人页面数据
     */
    @GetMapping("myinfo")
    public Object myinfo(@LoginUser Integer userId) {
        return wxQualificationsService.getQualificationsInfo(userId);
    }

    /**
     * 个人商户认证审批列表
     *
     * @param userId
     * @param status
     * @param page
     * @param limit
     * @param sort
     * @param order
     * @return
     */
    @GetMapping("mylist")
    public Object mylist(@LoginUser Integer userId,
                       Byte status,
                       @RequestParam(defaultValue = "1") Integer page,
                       @RequestParam(defaultValue = "10") Integer limit,
                       @Sort @RequestParam(defaultValue = "add_time") String sort,
                       @Order @RequestParam(defaultValue = "desc") String order) {
        if (userId == null) {
            return ResponseUtil.unlogin();
        }

        List<LitemallUserApprove> userApproveList = wxQualificationsService.queryList(userId, status, page, limit, sort, order);
        List<Map<String, Object>> userApproveVoList = change(userApproveList);
        return ResponseUtil.okList(userApproveVoList, userApproveList);
    }

    private List<Map<String, Object>> change(List<LitemallUserApprove> userApproveList) {
        List<Map<String, Object>> userApproveVoList = new ArrayList<>(userApproveList.size());
        for(LitemallUserApprove userApprove : userApproveList){
            Map<String, Object> vo = Maps.newHashMap();

            vo.put("id", userApprove.getId());
            vo.put("userType", userApprove.getUserType());
            vo.put("companyName", userApprove.getCompanyName());
            vo.put("addTime", userApprove.getAddTime());
            vo.put("approveStatus", userApprove.getApproveStatus());
            vo.put("approveTime", userApprove.getApproveTime());
            vo.put("approveRemark", userApprove.getApproveRemark());

            userApproveVoList.add(vo);
        }

        return userApproveVoList;
    }
    
    /**
     * 资质详情
     * @param userId
     * @param authId
     * @return
     */
    @GetMapping("detail")
    public Object detail(@LoginUser Integer userId, Integer authId, @NotNull Integer isAuth) {
        return wxQualificationsService.detail(userId, authId,isAuth);
    }
    
    /**
     * 查询目标状态数量
     * @param userId
     * @param status
     * @return
     */
    @GetMapping("couuntTargetStatus")
    public Object couuntTargetStatus(@LoginUser Integer userId, @NotNull Byte status) {
    	Long count = wxQualificationsService.countByUserIdAndStatus(userId, status);
    	return ResponseUtil.ok(count);
    }
}